<?php
class wwy_action
{
    public static $data = array();    //【$action_data】，由【$action】初始化（生成）的数据

    protected static $_view_status = 0;
    protected static $_view_message = '';
    protected static $_view_data = array();

    protected static  $_layout_data = array();


    protected $_view_dir = '';
    protected $_library_dir = '';
    protected $_layout_dir = '';

    protected $_layout = '';


    public function __construct()
    {

    }

    /* @start 主要流程控制方法 */
    public function run()
    {
        $this->_pre_run();

        $this->_current_page_run();

        $this->_post_run();
    }

    /**
     * 前置流程
     */
    protected function _pre_run()
    {
        $this->_init();
    }

    /**
     * 后置流程
     */
    protected function _post_run()
    {
        $this->_render();
    }

    /**
     * 每个子类继承后，自定义当前操作业务流程
     */
    protected function _current_page_run()
    {

    }


    /**
     * 初始化。如公共函数、资源容器等
     */
    protected function _init()
    {
        require_once PROJECT_ROOT_PATH . '/no_public/common/library/function/function.php';

        //初始化资源容器
        require_once PROJECT_ROOT_PATH . '/no_public/common/model/db_table/default.php';
        require_once PROJECT_ROOT_PATH . '/no_public/common/resource_container/common/model_container.php';
        require_once PROJECT_ROOT_PATH . '/no_public/common/resource_container/background/model_container.php';
        require_once PROJECT_ROOT_PATH . '/no_public/common/resource_container/foreground/model_container.php';
        require_once PROJECT_ROOT_PATH . '/no_public/common/resource_container/background/action_service_container.php';
        require_once PROJECT_ROOT_PATH . '/no_public/common/resource_container/foreground/action_service_container.php';
        require_once PROJECT_ROOT_PATH . '/no_public/common/resource_container/resource_container.php';
        wwy_resource_container::init();
    }

    /**
     * 渲染布局、合成整个页面，并输出
     */
    protected function _render()
    {
        $layout_content = $this->_render_layout($this->_layout);
        echo $layout_content;
    }
    /* @end 主要流程控制方法 */



    /* @start 公共辅助方法 */

    public static function set_view_status($view_status)
    {
        //一般只给【action_service】调用
        self::$_view_status = $view_status;
    }

    public static function get_view_status()
    {
        return self::$_view_status;
    }

    public static function set_view_message($view_message)
    {
        //一般只给【action_service】调用
        self::$_view_message = $view_message;
    }

    public static function get_view_message()
    {
        return self::$_view_message;
    }

    public static function set_view_data($view_data)
    {
        //一般只给【action_service】调用
        self::$_view_data = $view_data;
    }


    public static function add_view_data($key, $value)
    {
        //一般只给【action_service】调用
        self::$_view_data[$key] = $value;
    }

    public static function merge_view_data($view_data)
    {
        //一般只给【action_service】调用
        foreach ($view_data as $key => $value){
            self::add_view_data($key, $value);
        }
    }

    public static function get_view_result()
    {
        $view_result = array(
            'status'  => self::$_view_status,
            'message' => self::$_view_message,
            'data'    => self::$_view_data,
        );

        return $view_result;
    }





    /**
     * 渲染视图
     */
    protected function _render_view($view_file_path)
    {
        $view_dir = $this->_view_dir;

        $view_status  = self::$_view_status;
        $view_message = self::$_view_message;
        $view_data    = self::$_view_data;

        ob_start();
        require $view_dir . '/' . $view_file_path;

        $output = ob_get_clean();

        return $output;
    }

    /**
     * 渲染库
     */
    protected function _render_library($library_file_path)
    {
        $library_dir = $this->_library_dir;

        $view_status  = self::$_view_status;
        $view_message = self::$_view_message;
        $view_data    = self::$_view_data;

        ob_start();
        require $library_dir . '/' . $library_file_path;

        $output = ob_get_clean();

        return $output;
    }

    /**
     * 渲染布局
     */
    protected function _render_layout($layout_file_path)
    {
        $layout_dir = $this->_layout_dir;

        $view_status  = self::$_view_status;
        $view_message = self::$_view_message;
        $view_data    = self::$_view_data;
        $layout_data  = self::$_layout_data;

        ob_start();
        require $layout_dir . '/' . $layout_file_path;

        $output = ob_get_clean();

        return $output;
    }
    
    public function get_url_params($params = array(), $reset = false)
    {
        $old_params = (!$reset) ? $_GET : array();
    
        foreach ($params as $key => $value) {
            if ($value !== null) {
                $old_params[$key] = $value;
            } elseif (isset($old_params[$key])) {
                unset($old_params[$key]);
            }
        }
    
        $params = $old_params;
    
        if(!isset($params['route'])){
            $route = wwy_dispatcher::$route;
            $params = array_merge(array('route' => $route), $params);
        }
        
        return $params;
    }
    
    public function get_url_params_json($params = array(), $reset = false)
    {
        $params = $this->get_url_params($params, $reset);
        
        $params_json = json_encode($params);
    
        return $params_json;
    }
    
    public function get_url($params = array(), $reset = false)
    {                
        $params = $this->get_url_params($params, $reset);
        
        $query_str = http_build_query($params);
        //var_dump($query_str); exit();
        
        $url = '/?' . $query_str;
        
        return $url;
    }
    /* @end 公共辅助方法 */
}